<template>
	<om-addpage :scrollTop="scrollTop" style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				scrollTop: 0,
				editId: "",
				subDay: '',
				month: '',
				yearFlag: false,
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			getWorkParam() {
				let _that = this
				return {
					dataList: JSON.stringify(_that.subEditorObj['title_index'].workList),
					subDay: _that.subDay,
					month: _that.month,
					busCode:_that.busCode
				}
			},
			getWorkParam1() {
				let _that = this
				return {
					userId: _that.userId,
					instId: _that.$refs.autoform.formdata.instId,
				}
			},
			formSubmit() {
				let _that = this
				_that.formdata.yearFlag = _that.yearFlag
				_that.$refs.autoform.formdata.yearFlag = _that.yearFlag
				_that.om.formSave("tuserMonthlyPlan/addTuserMonthlyPlan", _that.formdata, _that)
			},
			verifyMonth(item) {
				let _that = this
				_that.month = Number(item.referNum)
				_that.om.request(
					"tuserMonthlyPlan/verifyMonth", {
						year: Number(_that.$refs.autoform.formdata.year),
						monthCode: item.code,
						editId: _that.editId ? _that.editId : ''
					},
					"post"
				).then(res => {
					if (res.succeed && res.value) {
						errorModal(_that, "提示信息", ignoreHtml(res.value), () => {
							_that.$refs.autoform.clear('title_basic', "monthCode")
						})
					} else {
						// _that.onSelectMonth(item)
					}
				})
			},
			onSelectMonth(item) {
				let _that = this
				_that.verifyMonth(item)
				_that.om.request(
					"tuserMonthlyPlan/getIndexList", {
						year: _that.$refs.autoform.formdata.year,
						monthCode: item.code
					},
					"post"
				).then(res => {
					_that.$refs.autoform.clear("title_mission", "mission")
					_that.$refs.autoform.clear("title_target", "target")
					if (res.value.target) {
						_that.$refs.autoform.formdata.target = res.value.target
						_that.$refs.autoform.formdata.mission = res.value.mission
					}
					let objFormCode = JSON.parse(uni.getStorageSync('objFormCode'))
					if (res.value.indexList) {
						if (Object.prototype.toString.call(res.value.indexList) === '[object Array]') {
							for (let index in res.value.indexList) {
								res.value.indexList[index].orderNum = objFormCode.DICT_CODE_R + (Number(index) + 1) || ''
								if (!res.value.indexList[index].deleteInMonthProj) {
									_that.$set(res.value.indexList[index], "showDel", false)
								}else{
									_that.$set(res.value.indexList[index], "showDel", true)
								}
							}
							_that.yearFlag = true
							uni.setStorageSync('objIndexList', "indexList")
							_that.$set(_that.subEditorObj["title_index"], "workList", res.value.indexList)
						} else {
							_that.yearFlag = false
							uni.setStorageSync('objIndexList', "")
							_that.$set(_that.subEditorObj["title_index"], "workList", [])
						}
					}
					if (res.value.workList) {
						if (Object.prototype.toString.call(res.value.workList) === '[object Array]') {
							for (let n in res.value.workList) {
								res.value.workList[n].orderNum = objFormCode.DICT_CODE_Y + (Number(n) + 1 || '')
							}
							_that.$set(_that.subEditorObj["title_work"], "workList", res.value.workList)
						} else {
							_that.$set(_that.subEditorObj["title_work"], "workList", [])
						}
					}
				})
			},
			getMonthDataList(item) {
				let _that = this
				_that.om.request(
					"dictZero/getRecentMonth", {
						year: item.name
					},
					"post"
				).then(res => {
					_that.$refs.autoform.changeEditor("title_basic", "monthCode", "data", configSelectData(res, "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("title_basic", "monthCode")
					if (!_that.edit) {
						_that.getDataList()
					}
				})
			},
			onSelectYear(item) {
				let _that = this
				let date = new Date()
				let nowYear = Number(date.getFullYear());
				if(item.value != nowYear){
					uni.showModal({
						title: '选择的' + nowYear + '年不是当前年，是否继续选择？',
						showCancel: true,
						success(res){
							if(res.confirm){
								_that.getMonthDataList(item)
							}else{
								_that.$refs.autoform.clear("title_basic", "year")
								_that.$refs.autoform.clear("title_basic", "monthCode")
							}
						}
					})
				}else{
					_that.getMonthDataList(item)
				}
			},
			getDataList() {
				let _that = this
				_that.om.request(
					"tmonthlyPlan/findAllIndexEchart", {
						year: _that.$refs.autoform.formdata.year,
						userId:_that.userId
					},
					"post"
				).then(res => {
					let arr = []
					for (let index in res.value) {
						let obj = {
							categories: [],
							series: [],
							title: "",
							type: 'line'
						}
						obj.categories = res.value[index].xAxis.data
						obj.series = res.value[index].series
						obj.title = res.value[index].title
						obj.name = res.value[index].yAxis.name
						obj.listTitle = ["折线图", "柱状图"]
						obj.listValue = ["line", "column"]
						arr.push(obj)
					}
					if(_that.subEditorObj["title_chart"]){
						_that.subEditorObj["title_chart"].workList = arr
					}
				})
			},
			initLoad(options) {
				let _that = this
				uni.setStorageSync('objIndexList', "")
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.dataDictInitValueUrl("DD021014001"),
					{
						url:"commonForm/getInitParamMap",
						param:{
							formCode:options.code
						},
					}
				]
				let fileId = options.fileId
				if (fileId) {
					ajaxArr.push({
						url: "tuserMonthlyPlan/getInfo",
						param: {
							id: fileId
						},
						method: "post"
					})
					_that.editId = fileId
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					uni.setStorageSync('objFormCode', JSON.stringify(res[2].value))
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					_that.subDay = res[1].value
					changeEditor("topinfo", "zuserId", {
						defaultValue: _that.userId,
						defaultName: _that.userName
					}, null, mainData)
					//年份
					let data = new Date()
					let year = data.getFullYear()
					let arr = [{
						name: Number(year),
						value: Number(year)
					}, {
						name: Number(year) + 1,
						value: Number(year) + 1
					}]
					changeEditor("title_basic", "year", "data", configSelectData(arr, "name", "value"), mainData)
					if (_that.subEditorObj["title_index"]) {
						changeEditor("title_index", "indexStr", {
							showDel:true,
							getWorkParam: "getWorkParam1",
							titleTemplate: "<%=indexName || '未填写'%>",
						}, null, mainData)
					}
					if (_that.subEditorObj["title_work"]) {
						changeEditor("title_work", "workStr", {
							getWorkParam: "getWorkParam",
							titleTemplate: "<%=workName || '未填写'%>",
						}, null, mainData)
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value.tuserMonthlyPlan) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value.tuserMonthlyPlan[index])
								_that.$set(_that.formdata, index, res[3].value.tuserMonthlyPlan[index])
							}
						}
						_that.$refs.autoform.formdata.target = res[3].value.target
						let dataList = _that._.cloneDeep(res[3].value.workList)
						_that.subEditorObj["title_work"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "workStr", JSON.stringify(dataList))
						_that.$set(_that.formdata, "workStr", JSON.stringify(dataList))

						let indexList = _that._.cloneDeep(res[3].value.indexList)
						for (let item of indexList) {
							if (!item.deleteInMonthProj) {
								_that.$set(item, "showDel", false)
							}else{
								_that.$set(item, "showDel",true)
							}
						}
						_that.subEditorObj["title_index"].workList = indexList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "indexStr", JSON.stringify(indexList))
						if(res[3].value.tuserMonthlyPlan.yearFlag){
							_that.yearFlag = true
							uni.setStorageSync('objIndexList', "indexList")
						}else{
							_that.yearFlag = false
							uni.setStorageSync('objIndexList', "")
						}
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.$refs.autoform.initSelectIndex("title_basic", "year")
							_that.getDataList()
							_that.getMonthDataList({
								name: _that.$refs.autoform.formdata.year
							})
							let month = _that.$refs.autoform.formdata.monthName.substring(0, _that.$refs.autoform.formdata.monthName.length -
								1)
							_that.month = month
						})

					}
				}, options)

			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			for (let fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId]
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						if (fieldId === "title_index") {
							for (let item of res.data) {
								if (item.logicRelationshipName) {
									if (item.targetValueCode) {
										item.targetValueName = item.logicRelationshipName + item.targetValueCode + item.indexUnitName
									}
									if (item.challengeTargetCode) {
										item.challengeTargetName = item.logicRelationshipName + item.challengeTargetCode + item.indexUnitName
									}
								} else if (item.prefix) {
									if (item.targetValueCode) {
										item.targetValueName = item.prefix + item.targetValueCode + item.suffix
									}
									if (item.challengeTargetCode) {
										item.challengeTargetName = item.prefix + item.challengeTargetCode + item.suffix
									}
								}
								if (!item.deleteInMonthProj) {
									_that.$set(item, "showDel", false)
								}else{
									_that.$set(item, "showDel", true)
								}
							}
						}
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 200) {
				this.scrollTop = e.scrollTop
			} else {
				this.scrollTop = 0
			}
		},
	}
</script>

<style></style>
